package com.kcs.fxmargin.action.form;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.kcs.common.utils.Constant;

public class FXActionForm extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String userLoginId;
	private String userLoginName;
	private String userLoginRoleId;
	
	private String errorMessage;
	
	public FXActionForm(){
		userLoginId 	= "";
		userLoginName 	= "";
		userLoginRoleId = "";
		errorMessage	= "";
	}
	
	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		Object loginDataSession = request.getSession().getAttribute(Constant.LOGIN_DATA_SESSION);
		
		try{
			System.out.println("::: reset :::");
			request.setCharacterEncoding("UTF-8");  
		}catch(Exception e){}
		
		if(loginDataSession != null){
			LoginActionForm loginForm = (LoginActionForm)loginDataSession;
			
			setUserLoginId(loginForm.getUserId());
			setUserLoginName(loginForm.getUserName());
			setUserLoginRoleId(loginForm.getRoleId());

		}else{
			//*** ERROR : Login Data Not Found
		}
		
		super.reset(mapping, request);
	}
	
	@SuppressWarnings("rawtypes")
	public void resetExpect() {
		Map valueMap = null;
		try {
			valueMap = PropertyUtils.describe(this);
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		if(valueMap != null) {
			for(Iterator iter = valueMap.keySet().iterator(); iter.hasNext(); ) {
		
				String currKey = (String) iter.next();
		
				if("class".equals(currKey)) {
					continue;//skip
				}
				Object currValue = valueMap.get(currKey);
		
				//System.out.println("currKey+currValue=" + currKey+":"+currValue);
				if(currValue != null) {
				//	                    if(currValue instanceof Integer || currValue == Integer.TYPE) {
				//	                        currValue = new Integer(0);
				//	                    }else if(currValue instanceof Long || currValue == Long.TYPE) {
				//	                        currValue = new Long(0);
				//	                    }else if(currValue instanceof Byte || currValue == Byte.TYPE) {
				//	                        currValue = new Byte("0");//beware
				//	                    }else if(currValue instanceof Short || currValue == Short.TYPE) {
				//	                        currValue = new Short("0");//beware
				//	                    }else if(currValue instanceof Double || currValue == Double.TYPE) {
				//	                        currValue = new Double(0);
				//	                    }else if(currValue instanceof Float || currValue == Float.TYPE) {
				//	                        currValue = new Float(0);
				//	                    }
				//if currValue is Boolean
				/*else*/
					if(currValue instanceof Boolean || currValue == Boolean.TYPE) {
						currValue = null; //for set isNull
					} else
					//if currValue is String
					if(currValue instanceof String) {
						currValue = null;
					} else { //another object
						currValue = null;
					}
					//set reset value
	
					try {
						BeanUtils.setProperty(this, currKey, currValue);
					} catch(Exception ex1) {
//						log.error(ex1);
					}
				}
			}
		}
	}

	public final String getUserLoginId() {
		return userLoginId;
	}

	public final void setUserLoginId(String userLoginId) {
		this.userLoginId = userLoginId;
	}

	public final String getUserLoginName() {
		return userLoginName;
	}

	public final void setUserLoginName(String userLoginName) {
		this.userLoginName = userLoginName;
	}

	public final String getUserLoginRoleId() {
		return userLoginRoleId;
	}

	public final void setUserLoginRoleId(String userLoginRoleId) {
		this.userLoginRoleId = userLoginRoleId;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
	
}
